package com.recSys.dataProcessing;

import com.recSys.util.HuiMinType;
import com.recSys.util.ResourcePathHandler;

import java.io.*;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * Created by dell on 2018/1/25.
 *  c_posID   1~4728
 *  c_age     4729
 *  c_sex     4730 4731  男 女
 *  d_sex     4732 4733
 *  d_age     4734
 *  d_posID   4735~9462
 *  d_HMtype  9463~9475
 *  d_Ethnic  9476~9533 民族
 *  d_Religon  9534~9540
 */
public class Deal4RecGoods {
    public static final String rootPath = ResourcePathHandler.getProgrameRootPath();
    static final String filePath4Goods = rootPath + "\\data\\initial\\deadInfo_initial.txt";
    static final String filePath4Goods_Out = rootPath + "\\data\\format\\deadInfo_format.txt";

    public static void formatDeadInfo4Goods() {
        try{
            int[] hmType = new HuiMinType().getHmType();
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Goods)),"UTF-8"));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath4Goods_Out)),"UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                String[] values = lineTxt.split("\t");
                bw.write(values[0] + "-"); //id
                bw.write(values[1] + ":1 "); //c_posID
                bw.write("4729:" + Integer.valueOf(values[2])/100.0 + " "); //c_age
                if(values[3].equals("1"))
                    bw.write("4730:1 "); //c_sex
                else
                    bw.write("4731:1 "); //c_sex
                if(values[4].equals("1"))
                    bw.write("4732:1 "); //d_sex
                else
                    bw.write("4733:1 "); //d_sex
                bw.write("4734:" + Integer.valueOf(values[5])/100.0 + " "); //d_age
                bw.write(4734 + Integer.valueOf(values[6]) + ":1 "); //d_posID
                bw.write(9462 + hmType[Integer.valueOf(values[7])] + ":1 "); //d_HMtype
                bw.write(9475 + Integer.valueOf(values[8]) + ":1 "); //d_Ethnic
                bw.write(9534 + Integer.valueOf(values[9]) + ":1 "); //d_Religon
                bw.newLine();
            }
            br.close();
            bw.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e.getMessage());
        }
    }

    public static TreeMap<Double,String> top10Similarity(String s1) {
        TreeMap<Double,String> map= new TreeMap<>(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                return -o1.compareTo(o2);
            }
        });
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Goods_Out)),"UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                String[] data = lineTxt.split("-");
                String s2 = data[1];
                double similarity = CosineSimilarity.evaluate(s1,s2);
                if(map.size() < 10) {
                    map.put(similarity,data[0]);
                } else {
                    if(similarity > map.lastKey()){
                        map.remove(map.lastKey());
                        if(map.containsKey(similarity)){
                            map.put(similarity,data[0] + " " + map.get(similarity));
                        } else {
                            map.put(similarity,data[0]);
                        }

                    }
                }
            }
            br.close();
            return map;
        } catch (Exception e) {
            System.err.println("read errors :" + e.getMessage());
            return map;
        }
    }

    public static void main(String[] args) {
        formatDeadInfo4Goods();
//        top10Similarity("");
    }

}
